from typing import Dict
import os

MEM_SCRIPT_RULES = [
    {"script": "mem_OOM Killer触发.sh", "keywords": ["oom", "内存不足", "killer"], "weight": 3},
    {"script": "mem_Slab内存泄漏.sh", "keywords": ["slab", "内存泄漏", "leak"], "weight": 2},
    {"script": "mem_内存耗尽大量换页.sh", "keywords": ["换页", "swap", "内存耗尽"], "weight": 2}
]

def select_script_for_anomaly(anomaly: Dict) -> str:
    root_cause = anomaly.get("root_cause", "").lower()
    evidence = " ".join(anomaly.get("evidence", [])).lower()
    severity = anomaly.get("severity", 0)
    best_score = -1
    best_script = MEM_SCRIPT_RULES[0]["script"]
    for rule in MEM_SCRIPT_RULES:
        score = 0
        for kw in rule["keywords"]:
            if kw in root_cause or kw in evidence:
                score += rule["weight"]
        if severity >= 8:
            score += 2
        elif severity >= 6:
            score += 1
        score += len(anomaly.get("evidence", [])) * 0.5
        if score > best_score:
            best_score = score
            best_script = rule["script"]
    return best_script 